package com.ymatou.iapi.idcardmng.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.idcardmng.parameter.SyncIdCardInfoFromQueryReq;
import com.ymatou.iapi.idcardmng.service.SyncIdCardInfoFromQueryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 身份证查询通知接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SyncIdCardInfoFromQuery {

	private static SyncIdCardInfoFromQueryReq bean;
	private static SyncIdCardInfoFromQueryCall call;

	private static String idPicRightSide = "http://file.iapi.ymatou.com/G01/M00/88/44/rBBlD1y2wnuAWojbAAA5iSbcoiY572.jpg";
	private static String idPicReverseSide = "http://file.iapi.ymatou.com/G01/M00/88/44/rBBlD1y2wnuAPTccAAA_1ZLCCuk104.jpg";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("身份证查询通知接口");
	}

	@Before
	public void caseUp() {
		bean = new SyncIdCardInfoFromQueryReq();
		call = new SyncIdCardInfoFromQueryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDb() throws Exception {

		Thread.sleep(1000);

		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();
		String oldKey = "1234567890123456";
		String newKey = "123456789";

		YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByNameAndId(
				bean.getName(),
				CipherUtil.encryptAES(oldKey, bean.getCardNumber()));

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		Logger.verifyEquals(bean.getName(), ymtIdCardInfo.getName(), "校验name");
		/*
		 * Logger.verifyEquals(bean.getCardNumber(),
		 * ymtIdCardInfo.getCardNumber(), "校验CardNumber");
		 */

		Logger.verifyEquals(bean.getCardType(), ymtIdCardInfo.getCardType(),
				"校验CardType");

		Logger.verifyEquals(bean.getStatus(), ymtIdCardInfo.getStatus(),
				"校验Status");

		Logger.verifyEquals(bean.isHasPic(), ymtIdCardInfo.getHasPic(),
				"校验HasPic");

		Logger.verifyEquals(
				bean.getMobile() != null ? CipherUtil.encryptAES(newKey,
						bean.getMobile()) : ymtIdCardInfo.getMobile(),
				ymtIdCardInfo.getMobile(), "校验Mobile");

		Logger.verifyEquals(
				CipherUtil.encryptAES(oldKey, bean.getCardNumber()),
				ymtIdCardInfo.getIDCard(), "校验IDCard");

		Logger.verifyEquals(
				ymtIdCardInfo.getPhone() != null ? CipherUtil.encryptAES(
						oldKey, bean.getMobile()) : null, ymtIdCardInfo
						.getPhone(), "校验phone");

		Logger.verifyEquals(0, ymtIdCardInfo.getIsDelete(), "校验IsDelete");

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SyncIdCardInfoFromQuery_001() {
		Logger.start(true, "身份证查询通知，本地如果没有数据记录，身份证服务插入新记录");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			bean.setBuyerId(buyerId);
			bean.setName(TestDataManager.getChineseName());
			bean.setCardNumber(TestDataManager.getCardId(20, 10));
			bean.setCardType(1);
			bean.setStatus(1);
			bean.setHasPic(true);
			bean.setMobile("136" + TestDataManager.getRandomNum(8));
			bean.setOperater("autotest");
			bean.setIdPicRightSide(idPicRightSide);
			bean.setIdPicReverseSide(idPicReverseSide);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证接口返回结果");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SyncIdCardInfoFromQuery_002() {
		Logger.start(true, "本地身份证记录存在，身份证查询通知成功,本地更新记录成功");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();
			;

			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = ymtIdCardInfo.getName();

			bean.setBuyerId(buyerId);
			bean.setName(name);
			bean.setCardNumber(TestDataManager.getCardId(20, 10));
			bean.setCardType(1);
			bean.setStatus(2);
			bean.setHasPic(true);
			bean.setMobile("136" + TestDataManager.getRandomNum(8));
			bean.setOperater("autotest");
			bean.setIdPicRightSide(idPicRightSide);
			bean.setIdPicReverseSide(idPicReverseSide);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证接口返回结果");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SyncIdCardInfoFromQuery_003() {
		Logger.start(true, "身份证查询通知，身份证状态为审核不通过 2,本地库更新成功并发送mq消息通知消息路由");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			bean.setBuyerId(buyerId);
			bean.setName(TestDataManager.getChineseName());
			bean.setCardNumber(TestDataManager.getCardId(20, 10));
			bean.setCardType(1);
			bean.setStatus(2);
			bean.setHasPic(true);
			bean.setMobile("136" + TestDataManager.getRandomNum(8));
			bean.setOperater("autotest");
			bean.setIdPicRightSide(idPicRightSide);
			bean.setIdPicReverseSide(idPicReverseSide);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证接口返回结果");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SyncIdCardInfoFromQuery_004() {
		Logger.start(true, "身份证查询通知状态 status为0");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			bean.setBuyerId(buyerId);
			bean.setName(TestDataManager.getChineseName());
			bean.setCardNumber(TestDataManager.getCardId(20, 10));
			bean.setCardType(1);
			bean.setStatus(0);
			bean.setHasPic(true);
			bean.setMobile("136" + TestDataManager.getRandomNum(8));
			bean.setOperater("autotest");
			bean.setIdPicRightSide(idPicRightSide);
			bean.setIdPicReverseSide(idPicReverseSide);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证接口返回结果");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SyncIdCardInfoFromQuery_005() {
		Logger.start(true, "买家id为null");
		try {

			// long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			// bean.setBuyerId(buyerId);
			bean.setName(TestDataManager.getChineseName());
			bean.setCardNumber(TestDataManager.getCardId(20, 10));
			bean.setCardType(1);
			bean.setStatus(0);
			bean.setHasPic(true);
			bean.setMobile("136" + TestDataManager.getRandomNum(8));
			bean.setOperater("autotest");
			bean.setIdPicRightSide(idPicRightSide);
			bean.setIdPicReverseSide(idPicReverseSide);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证接口返回结果");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SyncIdCardInfoFromQuery_006() {
		Logger.start(true, "name为null");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			bean.setBuyerId(buyerId);
			// bean.setName(TestDataManager.getChineseName());
			bean.setCardNumber(TestDataManager.getCardId(20, 10));
			bean.setCardType(1);
			bean.setStatus(0);
			bean.setHasPic(true);
			bean.setMobile("136" + TestDataManager.getRandomNum(8));
			bean.setOperater("autotest");
			bean.setIdPicRightSide(idPicRightSide);
			bean.setIdPicReverseSide(idPicReverseSide);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证接口返回结果");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SyncIdCardInfoFromQuery_007() {
		Logger.start(true, "CardNumber为null");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			bean.setBuyerId(buyerId);
			bean.setName(TestDataManager.getChineseName());
			// bean.setCardNumber(TestDataManager.getCardId(20, 10));
			bean.setCardType(1);
			bean.setStatus(0);
			bean.setHasPic(true);
			bean.setMobile("136" + TestDataManager.getRandomNum(8));
			bean.setOperater("autotest");
			bean.setIdPicRightSide(idPicRightSide);
			bean.setIdPicReverseSide(idPicReverseSide);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证接口返回结果");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SyncIdCardInfoFromQuery_008() {
		Logger.start(true, "mobile为null");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			bean.setBuyerId(buyerId);
			bean.setName(TestDataManager.getChineseName());
			bean.setCardNumber(TestDataManager.getCardId(20, 10));
			bean.setCardType(1);
			bean.setStatus(0);
			bean.setHasPic(true);
			// bean.setMobile("136" + TestDataManager.getRandomNum(8));
			bean.setOperater("autotest");
			bean.setIdPicRightSide(idPicRightSide);
			bean.setIdPicReverseSide(idPicReverseSide);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "验证接口返回结果");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}